package com.taobao.client.pages;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.taobao.client.RPC;
import com.taobao.client.sdk.RefundApplyRequest;
import com.taobao.client.util.PopBox;

/**
 * Apply the singleton design pattern.
 * A composite that displays the page for the buyer to apply a refund
 * 
 * @author hujingfei.pt
 *
 */
public class RefundApply extends Composite implements ClickHandler{
	
	private static RefundApply refund;		
	private final static String RESULT_FLAG = "ResultCode";	
	public final static String HISTORY_ITEM = "RefundPage";	
	// A vertical panel which contains various labels and textareas
//	private VerticalPanel container = new VerticalPanel();
//	private Label aucRemarkLabel = new Label("Auction Remark");
//	private TextArea aucRemarkTArea = new TextArea();
//	private Label ordRemarkLabel = new Label("Order Remark");
//	private TextArea ordRemarkTArea = new TextArea();
	private Button okButton = new Button("Apply Refund");
	// Parent trade remark.
	private String pTradeRemark;
	// Children trade remark.
	private String cTradeRemark;
	
	
	
	public RefundApplyRequest input;	
	
	public static RefundApply getRefundApply() {
		if (refund == null) {
			refund = new RefundApply();
		}
		return refund;
	}
	
	/**
	 * @param tradeId The trade id to set
	 * @throws Exception 
	 */
	public void setTradeId(String tradeId){
		input.setBiz_order_id(tradeId);			
	}
	
	@Override
	public void onClick(ClickEvent arg0) {
		if (arg0.getSource() == okButton) {
//			if (input == null) {
//				container.add(new Label("Login illegal, please login again"));
//			} else {
				
//			}				
		}
	}	
	
	public void simulate(){
		System.out.println("RefundApply::simulate");
		input.setAuction_remark(pTradeRemark);
		input.setOrder_remark(cTradeRemark);
		
		RPC.getService().getUrl(input.getURLMap(), new AsyncCallback<String>() {
			@Override
			public void onFailure(Throwable arg0) {
				PopBox.error("Get URL Error", arg0.getMessage());
			}

			@Override
			public void onSuccess(String url) {
//				Window.open(url, null, null);
				PopBox.openWindow(url);
			}
		});
	}
	
	

	/**
	 * Private constructor.
	 */
	private RefundApply() {
		String queryString = Window.Location.getQueryString();
		
		System.out.println("RefundApply::Query string:\t" + (queryString == null ? "null" : queryString) );
		
		if (queryString != null && queryString.indexOf(RESULT_FLAG) >= 0) {
			// TODO Process the result
		} else {
			initialize();
		}
	}

	/**
	 * Initialize the GUI
	 */
	private void initialize() {
//		container.add(createHead("申请退款"));
		
		FlexTable flexTable = new FlexTable();
		flexTable.setText(0, 0, "输入备注信息");
		flexTable.getCellFormatter().setHorizontalAlignment(0, 0, HorizontalPanel.ALIGN_CENTER);
		flexTable.getFlexCellFormatter().setColSpan(0, 0, 2);	
//		flexTable.setWidget(1, 0, aucRemarkLabel);
//		flexTable.setWidget(1, 1, aucRemarkTArea);
//		aucRemarkTArea.setSize("400px", "100px");
//		flexTable.setWidget(2, 0, ordRemarkLabel);
//		ordRemarkTArea.setSize("400px", "100px");
//		flexTable.setWidget(2, 1, ordRemarkTArea);
		flexTable.setWidget(3, 0, okButton);
		flexTable.getCellFormatter().setHorizontalAlignment(3, 0, HorizontalPanel.ALIGN_CENTER);
		flexTable.getFlexCellFormatter().setColSpan(3, 0, 2);	
		DecoratorPanel dPanel = new DecoratorPanel();
		dPanel.setWidget(flexTable);
//		container.add(dPanel);
		okButton.addClickHandler(this);

//		initWidget(container);
		
		// Initialize the input parameters
		input = new RefundApplyRequest();
		// Initialize the unchangeable parameters
		input.initialize();		
	}
	
	/**
	 * @return the pTradeRemark
	 */
	public String getpTradeRemark() {
		return pTradeRemark;
	}

	/**
	 * @param pTradeRemark the pTradeRemark to set
	 */
	public void setpTradeRemark(String pTradeRemark) {
		this.pTradeRemark = pTradeRemark;
	}

	/**
	 * @return the cTradeRemark
	 */
	public String getcTradeRemark() {
		return cTradeRemark;
	}

	/**
	 * @param cTradeRemark the cTradeRemark to set
	 */
	public void setcTradeRemark(String cTradeRemark) {
		this.cTradeRemark = cTradeRemark;
	}
}
